<template lang="html">
    <div class="container" v-loading="loading">
        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input class="form_input" v-model="searchText" placeholder="请输入编号或名称"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                  <el-button type="primary" size="small" @click="onAddtion">新增</el-button>
                  <el-button type="danger" :disabled="isDisabled" size="small" @click="onBatch">批量删除</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="tables">
          <el-table :data="tableData" @selection-change="handleSelectionChange" :row-class-name="rowStyle" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column prop="laceNumber" label="编号" align="center" width="200"></el-table-column>
                <el-table-column prop="laceName" label="名称" align="center" width="200"></el-table-column>
                <el-table-column prop="content" label="简介" align="center"></el-table-column>
                <el-table-column label="状态" align="center" width="100">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="300">
                    <template slot-scope="scope">
                          <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                          <el-button type="danger" size="mini" @click="fDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>

        <el-dialog title="新增蕾丝" v-loading="loading" :visible.sync="dialogVisible" append-to-body width="30%">
            <div class="form_item">
                <span> <i>*</i> 蕾丝名称</span>
                <el-input class="el_input" v-model="addForm.laceName"></el-input>
            </div>
            <div class="form_item">
                <span> <i>*</i> 蕾丝编号</span>
                <el-input class="el_input" v-model="addForm.laceNumber"></el-input>
            </div>
            <div class="form_item">
                <span>蕾丝简介</span>
                <el-input type="textarea" v-model="addForm.content"></el-input>
            </div>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>

<script>
export default {
  data(){
    return {
      loading: false,
      searchText: '',
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      currentData:[],
      tableData: [],
      dialogVisible: false,
      addForm: {},
    }
  },
  computed:{
    isDisabled:function() {
      return this.currentData.length>0?false:true;
    }
  },
  created(){
    this.selectCommodityLacePage();
  },
  methods:{
    async selectCommodityLacePage(){
      this.loading = true;
      let res = await this.$http.selectCommodityLacePage({
        query: this.searchText,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      });
      this.loading = false;
      this.tableData = res.data.rows;
      this.totalPage = res.data.total;
      this.pageSize = res.data.pageSize;
    },
    onAddtion(){
      this.addForm = {};
      this.dialogVisible = true;
    },
    fEdit(e){
      this.addForm = e;
      this.dialogVisible = true;
    },
    async updateCommodityMaterial(){
      let res = null;
      if(this.addForm.id){
        res = await this.$http.updateCommodityLace(this.addForm);
      }else{
        res = await this.$http.insertCommodityLace(this.addForm);
      }
      if(res.data.code == 0){
        this.$message.success(res.data.message);
        this.onSubmit();
        this.dialogVisible = false;
      }else{
        this.$message.error(res.data.message);
      }
    },
    confirm(){
      if(!this.addForm.laceName){
        this.$message.error('请输入蕾丝名称！！');
        return false;
      }else if(!this.addForm.laceNumber){
        this.$message.error('请输入蕾丝编号！！');
        return false;
      }
      this.updateCommodityMaterial();
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    onBatch(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('您确定要删除这些商品颜色吗？').then(res=>{
        return this.$http.deleteCommodityLaces({ids:aId.join(',')})
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success('删除成功');
          this.selectCommodityLacePage();
        }else{
          this.$message.error(res.data.message);
        }
      }).catch(e=>0);
    },
    fDelete(e){
      this.$confirm('您确定要删除这个商品颜色吗？').then(res=>{
         this.$http.deleteCommodityLace({id:e.id}).then(res=>{
             if(res.data.code == 0){
               this.$message.success('删除成功');
               this.selectCommodityLacePage();
             }else{
               this.$message.error(res.data.message);
             }
         })
      }).catch(e=>0);
    },
    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(JSON.stringify(row) == JSON.stringify(this.currentData[i])){
          return 'row_style';
        }
      }
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectCommodityLacePage();
    },
    onSwitch(e){
      this.$http.closeCommodityLace({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },

  }

}
</script>

<style lang="css" scoped>
/deep/.el-table .row_style{
  background-color: #f8f8f8 !important;
}
.color_block{
  width: 24px;
  height: 16px;
  margin: 0 auto;
}
i{font-style: normal;color: #FB3F3F;}
.form_item span{
  display: inline-block;
  width: 20%;
  text-align: right;
  font-size: 13px;
  color: #333333;
  margin-right: 10px;
}
.form_item .el-input,.form_item .el-textarea{
  width: 80%
}
.el_color{
  width: 80%;
  display: flex;
  align-items: center;
}
.el_color .c_pi{
  position: absolute;
  right: 0;
}
.el_color_wrap{
  position: relative;
}
.el_color span{
  width: 120px;
  text-align: left;
  text-indent: 1em;
  line-height: 32px;
  margin: 0;
  height: 32px;
  border-radius: 4px 0 0 4px;
  border: 1px solid #d8dce5;
  box-sizing: border-box;
}
.form_item{
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
</style>
